package bg.code.commons.builder.project;

import bg.code.commons.builder.utils.Strings;
import com.google.common.base.Objects;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.project.ProjectManager;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
 * @author 郑兴泉 956607644@qq.com
 * @data 2020/3/18
 * 描述：
 */
public class FileTemplateGroupConfigurable implements Configurable {
    private FileTemplateGroupConfigurableUI ui;
    private ToolConfigurationData configuration = ToolConfigurationData.getInstance();

    public FileTemplateGroupConfigurable() {
        this.ui = new FileTemplateGroupConfigurableUI(ProjectManager.getInstance().getDefaultProject(), this.configuration);
    }

    @Nls(
            capitalization = Nls.Capitalization.Title
    )
    @Override
    public String getDisplayName() {
        return "Nutz BG Code Generate";
    }

    @Nullable
    @Override
    public JComponent createComponent() {
        return this.ui.getRoot();
    }

    @Override
    public boolean isModified() {
        return !Objects.equal(this.configuration.getTemplatePath(), this.ui.getTemplatePath().getText()) || !Objects.equal(this.configuration.getUserMail(), this.ui.getUserMail().getText()) || !Objects.equal(this.configuration.getUserName(), this.ui.getUserName().getText());
    }

    @Override
    public void apply() throws ConfigurationException {
        if (Strings.isBlank(this.ui.getTemplatePath().getText())) {
            throw new ConfigurationException("请选择模板目录");
        } else if (Strings.isBlank(this.ui.getUserName().getText())) {
            throw new ConfigurationException("请输入姓名");
        } else if (Strings.isBlank(this.ui.getUserMail().getText())) {
            throw new ConfigurationException("请输入邮箱");
        } else {
            this.configuration.setTemplatePath(this.ui.getTemplatePath().getText());
            this.configuration.setUserMail(this.ui.getUserMail().getText());
            this.configuration.setUserName(this.ui.getUserName().getText());
        }
    }

    @Override
    public void reset() {
        this.ui.getUserName().setText(this.configuration.getUserName());
        this.ui.getUserMail().setText(this.configuration.getUserMail());
        this.ui.getTemplatePath().setText(this.configuration.getTemplatePath());
    }
}
